package com.lagou.bigdata.task01;

import java.util.List;
import java.util.Scanner;

public class StudentManager {

    // 集合存储学生信息
    private List<Student> students;
    private Scanner sc = new Scanner(System.in);

    public StudentManager(List<Student> students) {
        this.students = students;
    }

    // 增加学生信息
    public void addStudent() throws StudentIdException, AgeException {
        System.out.println("请输入学生信息（ 学号，姓名，年龄）:");
        String studentInfo = sc.next();
        String[] infoList = studentInfo.split(",");

        int id = Integer.parseInt(infoList[0]);

        // 检查学生信息是否存在
        for (Student student : students) {
            if (id == student.getId()) {
                System.out.println("该学生已经存在，添加失败!");
                return;
            }
        }

        // 当学生添加学生信息到集合中
        Student student = new Student(id, infoList[1], Integer.parseInt(infoList[2]));
        students.add(student);
        System.out.println("添加学生信息成功");
    }

    // 删除学生信息
    public void deleteStudent() {
        System.out.println("请输入学号:");
        int id = sc.nextInt();

        for (Student student : students) {
            if (id == student.getId()) {
                students.remove(student);
                System.out.println("删除学生信息成功");
                return;
            }
        }

        System.out.println("该学生信息不存在！");
    }

    // 修改学生信息
    public void modifyStudent() throws AgeException {
        System.out.println("请输入学号:");
        int id = sc.nextInt();

        for (Student student : students) {
            if (id == student.getId()) {
                System.out.println("请输入修改后的信息（姓名，年龄）:");
                String studentInfo = sc.next();
                String[] infoList = studentInfo.split(",");
                student.setName(infoList[0]);
                student.setAge(Integer.parseInt(infoList[1]));
                System.out.println("修改学生信息成功");
                return;
            }
            System.out.println("该学生信息不存在");
        }
    }

    // 查找学生
    public void searchStudent() {
        System.out.println("请输入学号:");
        int id = sc.nextInt();

        for (Student student : students) {
            if (id == student.getId()) {
                System.out.println("学号：" + student.getId() + " 姓名：" + student.getName() + " 年龄：" + student.getAge());
                return;
            }
            System.out.println("该学生信息不存在");
        }
    }

    // 遍历学生
    public void printStudents() {
        for (Student student : students) {
            System.out.println("学号：" + student.getId() + " 姓名：" + student.getName() + " 年龄：" + student.getAge());
        }
    }


}
